using System;
using System.IO;
using System.Windows.Forms;

namespace Elev.UI.Controls
{
    public partial class WelcomeScreen : UserControl
    {
        public event CreateButtonClickedEventHandler CreateButtonClicked;
        public event OpenButtonClickedEventHandler OpenButtonClicked;
        public event ReOpenButtonClickedEventHandler ReOpenButtonClicked;

        public WelcomeScreen()
        {
            InitializeComponent();
        }

        public void RefreshData()
        {
            FileInfo file;
            if (Globals.Get().LastOpenedFile != "")
            {
                file = new FileInfo(Globals.Get().LastOpenedFile);                
                if (file.Exists && !file.IsReadOnly)
                {
                    reopenButton.Enabled = true;
                    reopenLabel.Text = file.Name;
                    // Store the file path in the tag container
                    reopenLabel.Tag = file.FullName;
                }
                else
                {
                    // Deactivate this functionality
                    reopenButton.Enabled = false;
                    reopenLabel.Text = "";
                }
            }
            else
            {
                // First time the app is launched - show the SAMPLE.edb file, 
                // located in the installation folder
                string samplePath = Directory.GetCurrentDirectory();
                samplePath += Path.DirectorySeparatorChar + "EXEMPLE.edb";

                file = new FileInfo(samplePath);
                if (file.Exists && !file.IsReadOnly)
                {
                    reopenButton.Enabled = true;
                    reopenLabel.Text = file.Name;
                    // Store the file path in the tag container
                    reopenLabel.Tag = file.FullName;
                }
                else
                {
                    // Deactivate this functionality
                    reopenButton.Enabled = false;
                    reopenLabel.Text = "";
                }
            }
        }

        protected virtual void OnCreateButtonClicked()
        {
            CreateButtonClicked(this);
        }

        protected virtual void OnOpenButtonClicked()
        {
            OpenButtonClicked(this);
        }

        protected virtual void OnReOpenButtonClicked(string FilePath)
        {
            ReOpenButtonClicked(this, FilePath);
        }

        void createButton_Click(object sender, EventArgs e)
        {
            if (CreateButtonClicked != null)
                OnCreateButtonClicked();
        }

        void openButton_Click(object sender, EventArgs e)
        {
            if (OpenButtonClicked != null)
                OnOpenButtonClicked();
        }

        void reopenButton_Click(object sender, EventArgs e)
        {
            if (ReOpenButtonClicked != null)
            {
                OnReOpenButtonClicked((string)reopenLabel.Tag);
            }
        }
    }

    #region User control events

    public delegate void CreateButtonClickedEventHandler(object sender);
    public delegate void OpenButtonClickedEventHandler(object sender);
    public delegate void ReOpenButtonClickedEventHandler(object sender, string FilePath);

    #endregion
}